<?php

namespace App\Modules\CodeDesigner\DynamicProxyMode\Impls;

use App\Modules\CodeDesigner\DynamicProxyMode\MysqlPool;
use Hyperf\Database\MySqlConnection;
use Hyperf\Pool\Connection;

class MysqlPoolImpl implements MysqlPool
{

    /**
     * @var Connection
     */
    protected $instance;

    public function __construct(string $db, int $size = 0)
    {
        $config = config('database.connections.' . $db);
        $dsn = $config['driver'] . ':host=' . $config['host'] . ':' . $config['port'] . ';dbname=' . $config['database'];
        $pdo = new \PDO($dsn, $config['username'], $config['password']);
        $this->instance = new MySqlConnection($pdo, $config['database']);
    }

    public function get(): Connection
    {
        return $this->instance;
    }

    public function put(Connection $connection): void
    {
        $this->instance = $connection;
    }
}